/*
 * @(#)JdbcConfig.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.util.common;

import java.io.InputStream;
import java.util.Properties;

/**
 * JdbcConfig
 * 
 * @author BIS 2012-12-24
 */
public final class JdbcConfig
{

    /**
     * jdbcConfig
     */
    private static JdbcConfig jdbcConfig = null;

    /**
     * properties
     */
    private Properties        properties = null;

    /**
     * JdbcConfig
     * @param is
     * @param xml
     * @throws Exception
     */
    public JdbcConfig(InputStream is, boolean xml) throws Exception
    {
        properties = new Properties();
        if (xml)
        {
            properties.loadFromXML(is);
        }
        else
        {
            properties.load(is);
        }
    }

    /**
     * onload
     * @Description:
     * @param is
     * @param xml
     * @throws Exception
     */
    public static void onload(InputStream is, boolean xml) throws Exception
    {
        if (jdbcConfig == null)
        {
            jdbcConfig = new JdbcConfig(is, xml);
        }
    }

    /**
     * unload
     * @Description:
     */
    public static void unload()
    {
        if (jdbcConfig.properties != null)
        {
            jdbcConfig.properties.clear();
        }
        jdbcConfig = null;
    }

    /**
     * getValue
     * @Description:
     * @param key
     * @return
     */
    public static String getValue(String key)
    {
        try
        {
            return jdbcConfig.properties.getProperty(key);
        }
        catch (Exception ex)
        {
        }
        return null;
    }

    /**
     * getValue
     * @Description:
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getValue(String key, String defaultValue)
    {
        try
        {
            return jdbcConfig.properties.getProperty(key, defaultValue);
        }
        catch (Exception ex)
        {
            return defaultValue;
        }
    }

    /**
     * getInt
     * @Description:
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getInt(String key, int defaultValue)
    {
        try
        {
            return Integer.parseInt(jdbcConfig.properties.getProperty(key));
        }
        catch (Exception ex)
        {
            return defaultValue;
        }
    }

    /**
     * getBoolean
     * @Description:
     * @param key
     * @param defaultValue
     * @return
     */
    public static boolean getBoolean(String key, boolean defaultValue)
    {
        try
        {
            return Boolean.parseBoolean(jdbcConfig.properties.getProperty(key));
        }
        catch (Exception ex)
        {
            return defaultValue;
        }
    }

}
